<?php
/**
 * MPE个人中心 -- 首页
 * @Author Liuyi
 * @name MemberAction
 * @Create 2016-11-1 15:16:14
 * @Copyright MPE.Fiidee.Monda.Cn
 */
namespace member\action;
use herosphp\session\Session;
use herosphp\http\HttpRequest;
use herosphp\bean\Beans;
use herosphp\core\Loader;
use api\utils\PayApiClient;

class IndexAction extends CommonAction {

    protected $member_id=null;
    protected $dasu_id=null;

    public function __construct(){
        parent::__construct();
        $this->member_id = $this->_user->__get('id');
        $this->dasu_id = $this -> _user -> __get('dasu_pay_id');

    }

    public function index(HttpRequest $request){
		Session::start();
		$signInfo = $this->_user->getSignInfo();
		$level = 2;
		$level += ($signInfo['ischecked'] == 2) + ($signInfo['sign_status'] == 1) + (!!$signInfo['tradepass']);

        $orderService = Beans::get('trading.tradingOrder.service');
        $OrderModel = Loader::model('tradingOrder');
        $member_id = $this->member_id;
        $conditions = array();
        $conditions['member_id'] = $member_id;

        $items = $orderService->getOrderList($conditions,5,1);
        $items = $items['items'];
        foreach ($items as $k => $item) {

            $items[$k]['payway'] = $orderService->getPaywayText($item['pay_way']);
            $items[$k]['shippingway'] = $orderService->getShippingwayText($item['shipping_way']);
            $items[$k]['orderstatus'] = $orderService->getPaystatusText($item['order_status']);

        }

        //余额
        $data['userid'] = $this->dasu_id;
        $usable = $this->_user->__get('usable');//可用余额
        $url = "api/payment/getBalance";
        $result = PayApiClient::send($url, $data);
        if ($result->isSucess()){
            $res = $result->getData();
            $balance = $res['balance'];
        } else {
            $balance = 0;
        }
        $balance = setFloat($balance,2);
        $this->assign('balance',$balance);
        $this->assign('usable',$usable);
        $this->assign('photo',$this->_user->__get('photo'));
        $this->assign('items', $items);
        $this->assign('SHIPPING_WAY_BUYER', $OrderModel::SHIPPING_WAY_BUYER);
        $this->assign('PAY_WAY_ONLINE', $OrderModel::PAY_WAY_ONLINE);
        $this->assign('WAIT_FOR_MERCHANT_CONFIRMATION', $OrderModel::WAIT_FOR_MERCHANT_CONFIRMATION);
        $this->assign('WAIT_FOR_USER_CONFIRMATION', $OrderModel::WAIT_FOR_USER_CONFIRMATION);
        $this->assign('ORDER_STATUS_UNPAY', $OrderModel::ORDER_STATUS_UNPAY);
        $this->assign('ORDER_STATUS_TO_BE_SHIPPED', $OrderModel::ORDER_STATUS_TO_BE_SHIPPED);
        $this->assign('ORDER_STATUS_SHIPPED', $OrderModel::ORDER_STATUS_SHIPPED);
        $this->assign('ORDER_STATUS_COMPLETED', $OrderModel::ORDER_STATUS_COMPLETED);
        $this->assign('ORDER_STATUS_CANCELED', $OrderModel::ORDER_STATUS_CANCELED);
        $this->assign('level',$level);
		$this->assign('signInfo',$signInfo);
        $this -> setView('index/index');
    }

    /*此控制器主要用于前端展示已经有的公共css组件*/
    public function demo(HttpRequest $request){
        //$this -> setView('');
        $this -> display('index/demo');

    }

    /**
     * 未签约页面
     * @param HttpRequest $request
     */
    public function index_empty(HttpRequest $request) {
        $this->display('index/index_empty');
    }

}
